//
//  MTForgetPwdViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/13.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTForgetPwdViewController.h"
#import <SMS_SDK/SMSSDK.h>


@interface MTForgetPwdViewController ()
@property (weak, nonatomic) IBOutlet UIButton *getVeriCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *numFileld;
@property (weak, nonatomic) IBOutlet UITextField *pwdFileld;
@property (weak, nonatomic) IBOutlet UITextField *againPwdFileld;



@end

@implementation MTForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}


- (IBAction)dismissClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)getVeri:(id)sender {
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_numFileld.text zone:@"86"customIdentifier:nil result:^(NSError *error){
        error = nil;
        if (!error) {
            __block int timeout= 59; //倒计时时间
            
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            
            dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 *NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout <= 0){ //倒计时结束，关闭
                    dispatch_source_cancel(_timer);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        [self.getVeriCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                        self.getVeriCodeBtn.userInteractionEnabled = YES;
                    });
                } else {
                    
                    //            int minutes = timeout / 60;
                    
                    int seconds = timeout % 60;
                    
                    NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        //设置界面的按钮显示 根据自己需求设置
                        
                        [UIView beginAnimations:nil context:nil];
                        
                        [UIView setAnimationDuration:1];
                         [self.getVeriCodeBtn setTitleColor:UIColorFromRGB(0xffc000) forState:UIControlStateNormal];
                        [self.getVeriCodeBtn setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                        [self.getVeriCodeBtn setTintColor:UIColorFromRGB(0xffc000)];
                        [UIView commitAnimations];
                        
                        self.getVeriCodeBtn.userInteractionEnabled = NO;
                        
                    });
                    
                    timeout --;
                }
            });
            dispatch_resume(_timer);
        } else {
            [MBProgressHUD showError:@"请输入电话号码"];
          
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.view];
                NSLog(@"%@",error);

            });
        }
    }];

}

#pragma mark ------确定按钮点击
- (IBAction)confirmClick:(UIButton *)sender {
    if (self.pwdFileld.text != self.againPwdFileld.text) {
        [MBProgressHUD showError:@"密码输入不一致，请重新输入"];
    }
}

//监听键盘高度
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    //键盘高度
    [UIView animateWithDuration:0.1 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, -100);
    }];
    
}

-(void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [UIView animateWithDuration:0.1 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, 0);
    }];
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.1 animations:^{
        [self.view endEditing:YES];
    }];
    
}

@end
